<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <input type="text" id="username">
    <input type="password" id="password">
    <button id="login">发送</button>

    <script>
        //post传递的参数
        document.getElementById('login').onclick = function() {
            //获取用户输入的值
            var naem = document.getElementById('username').value;
            var psd = document.getElementById('password').value;

            //创建ajax
            var xhr = new XMLHttpRequest();
            xhr.open('post', '06ajax_post.php', true);
            //post的传递参数在send()发送参数
            var data = `username=${naem}&password=${psd}`;
            //配置请求
            xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=utf-8");
            xhr.send(data);
            xhr.onreadystatechange = function() {
                if (xhr.readyState == 4) {
                    if (xhr.status == 200) {
                        console.log(xhr.responseText);
                    }
                }
            }
        }
    </script>
</body>

</html>